new Vue({
    el:'#main',
    data:()=>({
        sale_id:$('#sale_id').val(),
        viewType:$('#viewType').val(),
        deliveryNoteId:$('#deliveryNoteId').val(),
        requestUrl:'',
        requestType:'index',
        page:1,
        pageSize:10,
        pageSum:0,
        saleData:{detail:[]},
        deliveryNote:{
            id:0,
            ctn:0,
            volume:0,
            sale_id:0,
            priority:0,
            delivery_type_id:0,
            co_id:0,
            co_branch_id:0,
            remark:'',
            detail:[
            ]
        },
        branchCompany:[],
        remarkTitle:'',
        remarkType:-1,
        delDetail_ids:'',
        
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(){
            $('.loding').show()
            this.pageSum = 0
            var data={
                requestType:this.requestType,
                viewType:this.viewType,
                deliveryNoteId:this.deliveryNoteId,
                sale_id:this.sale_id,
                page:this.page,
                pageSize:this.pageSize,
            }
            $.ajax({
                url:this.requestUrl,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    this.saleData = data.data.saleData
                    this.deliveryNote.co_id = data.data.saleData.co_id
                    this.deliveryNote.sale_id = data.data.saleData.id
                    this.deliveryNote.priority = data.data.saleData.priority
                    this.deliveryNote.delivery_type_id = data.data.saleData.delivery_type_id
                    this.branchCompany = data.data.branchCompany
                    if(this.viewType == 'edit') this.deliveryNote = data.data.deliveryNote
                    console.log(data)
                    if(data.data.pageSum){
                        this.pageSum = data.data.pageSum
                        this.Resetnav(this.page,data.data.pageSum)
                    }
                    $('.loding').hide()
                }
            })
        },
        // 直接把未送的数量 添加在右边对应产品的input
        addQty(key){
            var qty = this.saleData.detail[key].qty
            if(this.deliveryNote.detail.length > 0){
                var k = 0
                for (var i = 0; i < this.deliveryNote.detail.length; i++) {
                    if(this.deliveryNote.detail[i].pro_id == this.saleData.detail[key].pro_id && this.deliveryNote.detail[i].prochar_id == this.saleData.detail[key].prochar_id){
                        this.deliveryNote.detail[i].qty = 0
                        this.deliveryNote.detail[i].qty = this.saleData.detail[key].undone_qty
                        k = 1
                    }
                }
                if(k == 0){
                    this.addData(key)
                }    
            }else{
                this.addData(key)
            }
        },
        addData(key){
            var data = JSON.parse(JSON.stringify(this.saleData.detail[key]));
            data.qty = data.undone_qty
            this.deliveryNote.detail.push({
                id:0,
                sale_id:data.sale_id,
                pro_id:data.pro_id,
                prochar_id:data.prochar_id,
                qty:data.qty,
                proInfo:data.proInfo,
                remark:'',
            })
        },
        delData(key,detail_id){
            this.deliveryNote.detail.splice(key,1)
            if(this.detail_ids){
                this.delDetail_ids = this.delDetail_ids + ',' + detail_id
            }else{
                this.delDetail_ids = String(detail_id)
            }
        },
        // 备注
        addRemark(key,type=''){
            $('#remark_editModal .alert').hide()
            this.remarkType = key
            if(type=='add'){
                console.log(type)
                if(key == -1){
                    this.deliveryNote.remark = $('#remarkText').val()
                    var text = '销售单备注编辑成功'
                }else{
                    Vue.set(this.deliveryNote.detail[key],'remark', $('#remarkText').val());
                    this.deliveryNote.detail[key].remark = $('#remarkText').val()
                    var text = '销售单详情'+formatUnit(key+1,'id')+'备注编辑成功'
                }
                $('#remark_editModal').modal('toggle')
                this.point({'status':1,'msg':text},'#mainList')
                // this.point({'status':2,'msg':'编辑成功'},'#remark_editModal')

            }else{
                if(key == -1){
                    this.remarkTitle = '送货单备注编辑'
                    $('#remarkText').val(this.deliveryNote.remark)
                }else{
                    this.remarkTitle = '送货单详情备注编辑'
                    $('#remarkText').val(this.deliveryNote.detail[key].remark)
                }
            }
            // console.log(this.deliveryNote)
            
        },
        saveFun(){
            this.countQty()
            var data={
                requestType:'save',
                data:this.deliveryNote,
                viewType:this.viewType,
                delDetail_ids:this.delDetail_ids,
            }
            console.log(data)
            $.ajax({
                url:this.requestUrl,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    console.log(data)
                    this.point(data,'#mainList');
                }
            })
        },
        countQty(){
            if(this.deliveryNote.detail.length > 0){
                for (var i = 0; i < this.deliveryNote.detail.length; i++) {
                    this.deliveryNote.ctn += Math.ceil(this.deliveryNote.detail[i].qty/this.deliveryNote.detail[i].proInfo.prochar_qty)
                    this.deliveryNote.volume += Math.round(this.deliveryNote.ctn*this.deliveryNote.detail[i].proInfo.ctn_m3*1000)/1000
                }
            }
        },











        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        point(data,modelId){
            change_mes_type(modelId+' .alert',data.status)
            if(data.status == 1){
                $(modelId+' .alert').show()
                $(modelId+' .alert p').html('*'+data.msg+'<br>')
            }else{
                $(modelId+' .alert').show()
                $(modelId+' .alert p').html('*'+data.msg+'<br>')
            }
        },
        // 返回上一页面
        historyBack(){
            window.history.back();
        },
        //设置popper组件的配置
        setpopper(e){
            $(e.target).popover({
                trigger: 'hover focus manual',
                placement: ($(e.target).attr("placement") == undefined) ? 'left' : $(e.target).attr("placement"),
                //placement:'left',
                title: $(e.target).attr("title"),
                html: 'true',
                content: $(e.target).find(".content").html(),
                animation: false,
            });
            // 改了底层代码：bootstrap.js  1845行新增代码：o.content =  $e.children().html()
            
        },

    },
    mounted(){
        this.requestUrl = "/admin/sale/deliveryNoteList";
        this.getdata()
        $('#element').popover('destroy')
    },
})
$("#remark_editModal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
});